package com.test.servicetest;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bosssoft.itfinance.citizen.demo.biz.entity.Permission;
import com.bosssoft.itfinance.citizen.demo.biz.entity.Role;
import com.bosssoft.itfinance.citizen.demo.biz.service.IPermissionService;
import com.bosssoft.itfinance.citizen.demo.biz.service.IRoleService;
import com.test.common.BaseTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 日常数据库操作案例
 * Created by wangml on 2017/8/28.
 */
public class DailyDemo extends BaseTest{
    private static final Logger logger = LoggerFactory.getLogger(DailyDemo.class);

    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IRoleService roleService;

    /**
     * 通过ID查询
     * @throws Exception
     */
    @Test
    public void queryById() throws Exception {
        Permission permission = permissionService.selectById("1");
        logger.error("========="+ JSON.toJSON(permission));
    }

    /**
     * 复杂查询
     * 通过实体映射 | 模糊查询 | 区间大小查询 | 聚合 | 排序
     * 注意实体映射不能和包装类同时使用
     * @throws Exception
     */
    @Test
    public void query() throws Exception {
        EntityWrapper<Permission> ew = new EntityWrapper<>();
/*        Permission permission = new Permission();
        permission.setId("1");
        ew.setEntity(permission);*/
        ew.like(Permission.PERMISSION_NAME,"管理");
        ew.eq(Permission.STATE,1);
        ew.lt(Permission.SEQNUM,2);
        ew.setSqlSelect("pid,count(1) as seqnum");
        ew.groupBy(Permission.PID);
        ew.orderBy(Permission.PID);
        List<Permission> list = permissionService.selectList(ew);
        logger.error("========="+ JSON.toJSON(list));
    }

    /**
     * 分页查询
     * @throws Exception
     */
    @Test
    public void queryForPage() throws Exception {
        Page<Permission> page = new Page<>();
        page.setCurrent(1);
        page.setSize(10);
        EntityWrapper<Permission> ew = new EntityWrapper<>();
        ew.like(Permission.PERMISSION_NAME,"管理");
        ew.eq(Permission.STATE,1);

        Page<Permission> pageRst = permissionService.selectPage(page,ew);
        logger.error("========="+JSON.toJSON(pageRst));
    }

    /**
     * 更新
     * @throws Exception
     */
    @Test
    public void updateById() throws Exception {
        Permission permission = new Permission();
        permission.setId("1");
        permission.setType("1");
        if(permissionService.updateById(permission)){
            logger.error("更新成功");
        }else{
            logger.error("更新失败");
        }
    }

    /**
     * 事务处理
     * 注意，在同一个类中有事务方法调用无事务方法是生效的，反之不生效（无事务控制）
     * @throws Exception
     */
    @Test
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public void transationTest() throws Exception {
        doProcess();
    }

    private void doProcess() throws Exception{
        Permission permission = new Permission();
        permission.setId("1");
        permission.setPermissionName("系统管理3");
        if(permissionService.updateById(permission)){
            logger.error("权限更新成功");
           throw new Exception("测试异常");
        }else{
            logger.error("权限更新失败");
        }

        Role role = new Role();
        role.setId("2");
        role.setNote("222");
        if(roleService.updateById(role)){
            logger.error("角色更新成功");
        }
    }

}
